<?php
class PurchaseApp extends MemberbaseApp
{
	var $_sourcing_mod;
	var $_bulk_mod;
    function __construct()
    {
        $this->PurchaseApp();
    }
    function PurchaseApp()
    {
        parent::__construct();
        $this->_sourcing_mod=&m('sourcing_info');
        $this->_bulk_mod=&m('bulk_sourcing');
        $user=$this->visitor->get();
        $this->assign('user',$user);
    }
    
    //采购头板
	function index(){
		$user_info=$this->visitor->get();
		$sid=!empty($_GET['sid'])?intval($_GET['sid']):0;
		$page = $this->_get_page(20);
		if ($user_info['role']==USER_SAMPLE){
			$conditions.="sourcing.order_id=0 and sourcing.sample_id<>0 and sourcing_status ".db_create_in(array(SOURCING_STATUS_CREATE,SOURCING_STATUS_SOUR_SUBMIT));
		}elseif ($user_info['role']==USER_SOURCING){
			$conditions.="sourcing.order_id=0 and sourcing.sample_id<>0 and sourcing_status ".db_create_in(array(SOURCING_STATUS_CREATE,SOURCING_STATUS_SOUR_SUBMIT));
		}elseif ($user_info['role']==USER_FINANCE){
			$conditions.="sourcing.order_id=0 and sourcing.sample_id<>0 and sourcing_status ".db_create_in(array(SOURCING_STATUS_FINANCE_EXAMINE,SOURCING_STATUS_SAMPLE_CONFIRM));
		}elseif (in_array($user_info['role'], array(USER_MERCHANDISER))){
			$conditions.="sourcing.order_id=0 and sourcing.sample_id<>0 and sample.merchandiser_id=".$user_info['user_id'].' and sourcing_status !='.SOURCING_STATUS_FINANCE_EXAMINE;
		}elseif (in_array($user_info['role'], array(USER_MERCHANDISER_ADMIN))){
			$conditions.="sourcing.order_id=0 and sourcing.sample_id<>0 and sourcing_status !=".SOURCING_STATUS_FINANCE_EXAMINE;
		}
		if ($sid){
			$conditions .=" and sourcing.sample_id=".$sid;
		}
		if (in_array($user_info['role'], array(USER_SAMPLE,USER_SOURCING,USER_FINANCE,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN))){
			$conditions .=" and status != " . SAMPLE_STATUS_DELETE;
			$sour_list = $this->_sourcing_mod->get_sourcing_list($conditions,'add_time desc',$page['limit']);
		}
		$page['item_count'] = $this->_sourcing_mod->getCount();
		$this->_format_page($page);
		$this->assign('sid',$sid);
		$this->assign('page_info', $page);
		$this->assign('sour_list', $sour_list);
			
		$this->_curlocal(Lang::get('purchase_list'));
		$this->display('purchase.index.html');
	}
	
	//采购生产板
	function status(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(20);
		if ($user_info['role']==USER_SAMPLE){
			$conditions.="sourcing.order_id<>0 and sourcing.sample_id=0 and sourcing_status ".db_create_in(array(SOURCING_STATUS_CREATE,SOURCING_STATUS_SOUR_SUBMIT));
		}elseif ($user_info['role']==USER_SOURCING){
			$conditions.="sourcing.order_id<>0 and sourcing.sample_id=0 and sourcing_status ".db_create_in(array(SOURCING_STATUS_CREATE,SOURCING_STATUS_SOUR_SUBMIT));
		}elseif ($user_info['role']==USER_FINANCE){
			$conditions.="sourcing.order_id<>0 and sourcing.sample_id=0 and sourcing_status ".db_create_in(array(SOURCING_STATUS_SAMPLE_CONFIRM));
		}elseif (in_array($user_info['role'], array(USER_MERCHANDISER))){
			$conditions.="sourcing.order_id<>0 and sourcing.sample_id=0 and merchandiser_id=".$user_info['user_id']." and sourcing_status !=".SOURCING_STATUS_FINANCE_EXAMINE;
		}elseif (in_array($user_info['role'], array(USER_MERCHANDISER_ADMIN))){
			$conditions.="sourcing.order_id<>0 and sourcing.sample_id=0 and sourcing_status !=".SOURCING_STATUS_FINANCE_EXAMINE;;
		}
		if (in_array($user_info['role'], array(USER_SAMPLE,USER_SOURCING,USER_FINANCE,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN))){
			//$conditions .=" and status != " . SAMPLE_STATUS_DELETE;
			$sour_list = $this->_sourcing_mod->get_sourcing_status_list($conditions,'add_time desc',$page['limit']);
		}
		$page['item_count'] = $this->_sourcing_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('sour_list', $sour_list);
			
		$this->_curlocal(Lang::get('purchase_order_list'));
		$this->display('purchase.status.html');
	}
	
	
	//大货采购单查询
	function bulk(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(20);
		if ($user_info['role']==USER_SAMPLE){
			$conditions.="1=1";
		}elseif ($user_info['role']==USER_SOURCING){
			$conditions.="bulk_status ".db_create_in(array(BULK_STATUS_FINANCE_APPROVE,BULK_STATUS_CONFIRM));
		}elseif ($user_info['role']==USER_FINANCE){
			$conditions.="bulk_status ".db_create_in(array(BULK_STATUS_OUTGOING_CONFIRM,BULK_STATUS_FINANCE_APPROVE));
		}elseif (in_array($user_info['role'], array(USER_MERCHANDISER))){
			$conditions.="merchandiser_id={$user_info['user_id']} and bulk_status ".db_create_in(array(BULK_STATUS_CREATE,BULK_STATUS_SEND_MANAGER));
		}elseif (in_array($user_info['role'], array(USER_MERCHANDISER_ADMIN))){
			$conditions.="bulk_status ".db_create_in(array(BULK_STATUS_SEND_MANAGER,BULK_STATUS_MANAGER_APPROVE));
		}elseif (in_array($user_info['role'], array(USER_OUTGOING))){
			$conditions.="bulk_status ".db_create_in(array(BULK_STATUS_MANAGER_APPROVE,BULK_STATUS_OUTGOING_CONFIRM));
		}
		if (in_array($user_info['role'], array(USER_SAMPLE,USER_SOURCING,USER_FINANCE,USER_MERCHANDISER,USER_MERCHANDISER_ADMIN,USER_OUTGOING))){
			//$conditions .=" and status != " . SAMPLE_STATUS_DELETE;
			$sour_list = $this->_bulk_mod->get_bulk_sourcing_list($conditions,'bulk_time desc',$page['limit']);
		}
		$page['item_count'] = $this->_bulk_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('sour_list', $sour_list);
			
		$this->_curlocal(Lang::get('purchase_bulk_list'));
		$this->display('purchase.bulk.html');
	}
	
	//生产板详情
	function order_sour(){
		$sour_id=isset($_GET['id'])?intval($_GET['id']):0;
		$view=isset($_GET['print'])?trim($_GET['print']):'';
		if (!$sour_id){
			$this->show_warning('no_such_sour');
			return;
		}
		if ($view=='view'){
			$this->assign('is_view',true);
		}
		$sour_info=$this->_sourcing_mod->get(array(
			'join'=>'belongs_order_sour',
			'conditions'=>'sourcing_id='.$sour_id		
		));
		if (empty($sour_info)) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		$fabric_order_mod=& m('fabric_order');
		$fabric_info_mod=& m('fabric_info');
		if ($sour_info['order_id']){
			$fabric_arr_id=array();
			$fabric_status_list=$fabric_order_mod->find(array(
					'conditions'=>'order_id='.$sour_info['order_id'].' and sourcing_id='.$sour_id,
					'fields'=>"fabric_order.fabric_info_id,fabric_order.description,fabric_order.fabric_shop_id,item,price,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id,fabric_characteristics",
					'join'	=>"belongs_fabric_shop",
					'order'=>'fabric_info_id ASC'
			));
			if (empty($fabric_status_list)){
				if (!empty($sour_info['sample_id'])){
					$fabric_status_list = $fabric_info_mod->find(array(
							'conditions'=>'sample_id='.$sour_info['sample_id'],
							'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id,fabric_characteristics",
							'join'	=>"belongs_fabric_shop",
							'order'=>'fabric_info_id ASC'
					));
					if (!empty($fabric_status_list)){
						foreach ($fabric_status_list as $keys=>$res){
							$fabric_status_list[$keys]['fabric_info_id']=0;
						}
					}
				}
			}else{
				foreach ($fabric_status_list as $rows){
					$fabric_arr_id[]=$rows['fabric_info_id'];
				}
			}
			$this->assign('fabric_info_list',$fabric_status_list);
		}
		
		if (!IS_POST){
			$colorcard_mode=& m('color_card');
			$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");
			
			$this->assign("fabric_shop_list",$fabric_shop_list);
			$this->assign('sours',$sour_info);
			$this->display('purchase.sour.html');
		}else{
			$submit_val= trim($_POST['submit']);
			
			$mod_pro_records=&m("pro_records");
			$log_time=gmtime();
				
			if ($submit_val=="确认"){
				$this->_sourcing_mod->edit($sour_id,array('sourcing_status'=>SOURCING_STATUS_SAMPLE_CONFIRM));
				if ($this->_sourcing_mod->has_error()){
					$this->show_warning($this->_sourcing_mod->get_error());
					return;
				}
			}elseif ($submit_val=="重置"){
				$pro_data=array(
						"type"=>PRO_RECORDS_ORDER,
						"key_id"=>$sour_info['order_id'],
						"rec_name"=>$this->visitor->get("user_name"),
						"rec_buff"=>"重置成本采购单",
						"rec_time"=>$log_time
				);
				if(!$mod_pro_records->add($pro_data))
				{
					$this->show_warning($mod_pro_records->get_error());
				}
				if (!empty($sour_info)){
					$this->_sourcing_mod->edit($sour_id,array('sourcing_status'=>SOURCING_STATUS_CREATE,'if_print'=>0));
					if ($this->_sourcing_mod->has_error()){
						$this->show_warning($this->_sourcing_mod->get_error());
						return;
					}
				}
			}elseif ($submit_val=="提交"){
				$this->_sourcing_mod->edit($sour_id,array('sourcing_status'=>SOURCING_STATUS_SOUR_SUBMIT));
				if ($this->_sourcing_mod->has_error()){
					$this->show_warning($this->_sourcing_mod->get_error());
					return;
				}
			}elseif ($submit_val=="财务审核"){
				//$data['status']=SAMPLE_STATUS_FACTORY_SUBMIT;
				$this->_sourcing_mod->edit($sour_id,array('sourcing_status'=>SOURCING_STATUS_FINANCE_EXAMINE));
				if ($this->_sourcing_mod->has_error()){
					$this->show_warning($this->_sourcing_mod->get_error());
					return;
				}
			}
				
			$new_fabric_ids=array();
			if ($_POST['price']){
				foreach($_POST['price'] as $key=>$item)
				{
					$fabric_info_id=isset($_POST['fabric_info_id'][$key])?intval($_POST['fabric_info_id'][$key]):0;
					$fabric_data=array(
							"sourcing_id"=>$sour_info['sourcing_id'],
							"order_id"=>$sour_info['order_id'],
							"description"=>isset($_POST['description'][$key])?trim($_POST['description'][$key]):"",
							"fabric_shop_id"=>isset($_POST['fabric_shop_name'][$key])?intval($_POST['fabric_shop_name'][$key]):"",
							"cc_id"=>isset($_POST['cc_id'][$key])?intval($_POST['cc_id'][$key]):0,
							"item"=>isset($_POST['item'][$key])?trim($_POST['item'][$key]):"",
							"price"=>isset($_POST['price'][$key])?trim($_POST['price'][$key]):"",
							"colors"=>isset($_POST['colors'][$key])?trim($_POST['colors'][$key]):"",
							"fabric_num"=>isset($_POST['fabric_num'][$key])?trim($_POST['fabric_num'][$key]):"",
							"address"=>isset($_POST['address'][$key])?trim($_POST['address'][$key]):"",
							"phone_mob"=>isset($_POST['phone_mob'][$key])?trim($_POST['phone_mob'][$key]):"",
					);
						
					if(!empty($fabric_data['price']))
					{
						if(empty($fabric_info_id) || !in_array($fabric_info_id, $fabric_arr_id))
						{
							$fabric_order_mod->add($fabric_data);
						}
						else
						{
							$new_fabric_ids[]=$fabric_info_id;
								
							$fabric_order_mod->edit("fabric_info_id={$fabric_info_id}",$fabric_data);
						}
					}
				}
			}
			foreach($fabric_status_list as $item)
			{
				if(!in_array($item['fabric_info_id'],$new_fabric_ids))
				{
					$fabric_order_mod->drop("fabric_info_id={$item['fabric_info_id']}");
				}
			}
			$this->show_message('handle_success',Lang::get('order_sour_info'),'index.php?app=purchase&act=order_sour&id='.$sour_id);
		}
	}
	
	
	//大货采购单详情
	function bulk_sour(){
		$sour_id=isset($_GET['id'])?intval($_GET['id']):0;
		$view=isset($_GET['print'])?trim($_GET['print']):'';
		$user=$this->visitor->get();
		if (!$sour_id){
			$this->show_warning('no_such_sour');
			return;
		}
		if ($view=='view'){
			$this->assign('is_view',true);
		}
		$sour_info=$this->_bulk_mod->get(array(
				'join'=>'belongs_to_bulk',
				'conditions'=>'bulk_id='.$sour_id
		));
		if (empty($sour_info)) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		$fabric_order_mod=& m('fabric_order');
		$fabric_info_mod=& m('fabric_info');
		if ($sour_info['order_id']){
			$fabric_arr_id=array();
			$fabric_status_list=$fabric_info_mod->find(array(
					'conditions'=>'order_id='.$sour_info['order_id'].' and bulk_id='.$sour_id,
					'fields'=>"fabric_info.fabric_info_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id,fabric_characteristics",
					'join'	=>"belongs_fabric_shop",
					'order'=>'fabric_info_id ASC'
			));
			$sour_list=$this->_sourcing_mod->get(array(
					'conditions'=>'order_id='.$sour_info['order_id'],
					'order'=>'add_time desc',
					'fields'=>'sourcing_id'
			));
			if ($sour_info['sample_id']){
				$sample_list=$this->_sourcing_mod->get(array(
						'conditions'=>'sample_id='.$sour_info['sample_id'],
						'fields'=>'sample_id'
				));
			}
			if (empty($fabric_status_list)){
				if (!empty($sour_list)){
					$fabric_status_list=$fabric_order_mod->find(array(
							'conditions'=>'order_id='.$sour_info['order_id'].' and sourcing_id='.$sour_list['sourcing_id'],
							'fields'=>"fabric_order.fabric_info_id,fabric_order.description,fabric_order.fabric_shop_id,item,price,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id,fabric_characteristics",
							'join'	=>"belongs_fabric_shop",
							'order'=>'fabric_info_id ASC'
					));
					if (empty($fabric_status_list)){
						if (!empty($sample_list)){
							$fabric_status_list=$fabric_info_mod->find(array(
									'conditions'=>'sample_id='.$sample_list['sample_id'],
									'fields'=>"fabric_info.fabric_info_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id,fabric_characteristics",
									'join'	=>"belongs_fabric_shop",
									'order'=>'fabric_info_id ASC'
							));
						}
					}
					if (!empty($fabric_status_list)){
						foreach ($fabric_status_list as $keys=>$res){
							$fabric_status_list[$keys]['fabric_info_id']=0;
						}
					}
				}
				
				if (!empty($sour_info['sample_id'])){
					$fabric_status_list = $fabric_info_mod->find(array(
							'conditions'=>'sample_id='.$sour_info['sample_id'],
							'fields'=>"fabric_info.fabric_info_id,sample_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id,fabric_characteristics",
							'join'	=>"belongs_fabric_shop",
							'order'=>'fabric_info_id ASC'
					));
					if (!empty($fabric_status_list)){
						foreach ($fabric_status_list as $keys=>$res){
							$fabric_status_list[$keys]['fabric_info_id']=0;
						}
					}
				}else{
					if (!empty($sample_list)){
						$fabric_status_list=$fabric_info_mod->find(array(
								'conditions'=>'sample_id='.$sample_list['sample_id'],
								'fields'=>"fabric_info.fabric_info_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id,fabric_characteristics",
								'join'	=>"belongs_fabric_shop",
								'order'=>'fabric_info_id ASC'
						));
						if (!empty($fabric_status_list)){
							foreach ($fabric_status_list as $keys=>$res){
								$fabric_status_list[$keys]['fabric_info_id']=0;
							}
						}
					}
				}
			}else{
				foreach ($fabric_status_list as $rows){
					$fabric_arr_id[]=$rows['fabric_info_id'];
				}
			}
			$this->assign('fabric_info_list',$fabric_status_list);
		}
	
		if (!IS_POST){
			$colorcard_mode=& m('color_card');
			$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");
				
			$this->assign("fabric_shop_list",$fabric_shop_list);
			$this->assign('sours',$sour_info);
			$this->display('purchase_bulk.form.html');
		}else{
			$submit_val= trim($_POST['submit']);
			$bulk_desction=isset($_POST['bulk_desction'])?trim($_POST['bulk_desction']):"";
			$approve_no_desc=isset($_POST['approve_no_desc'])?trim($_POST['approve_no_desc']):"";
			$data=array();
			switch ($sour_info['bulk_status']) {
				case BULK_STATUS_CREATE:
					if ($submit_val=="送主管审批"){
						if ($user['role']==USER_MERCHANDISER){
							$data['bulk_status']=BULK_STATUS_SEND_MANAGER;
						}
					}elseif ($submit_val=="保存"){
						if ($user['role']==USER_MERCHANDISER){
							$data['bulk_status']=BULK_STATUS_CREATE;
						}
					}
					break;
				case BULK_STATUS_SEND_MANAGER:
					if ($submit_val=="审批通过"){
						if ($user['role']==USER_MERCHANDISER_ADMIN){
							$data['bulk_status']=BULK_STATUS_MANAGER_APPROVE;
						}
					}elseif ($submit_val=="审批不通过"){
						if ($user['role']==USER_MERCHANDISER_ADMIN){
							if (empty($approve_no_desc)) {
								$this->show_warning('请输入审批不通过理由');
								return;
							}
							$data['bulk_status']=BULK_STATUS_CREATE;
						}
					}elseif ($submit_val=="送主管审批"){
						if ($user['role']==USER_MERCHANDISER){
							$data['bulk_status']=BULK_STATUS_SEND_MANAGER;
						}
					}elseif ($submit_val=="保存"){
						if ($user['role']==USER_MERCHANDISER){
							$data['bulk_status']=BULK_STATUS_SEND_MANAGER;
						}
					}
					break;
				case BULK_STATUS_MANAGER_APPROVE:
					if ($submit_val=="主管确认"){
						if ($user['role']==USER_OUTGOING){
							$data['bulk_status']=BULK_STATUS_OUTGOING_CONFIRM;
						}
					}elseif ($submit_val=="退回"){
						if ($user['role']==USER_OUTGOING){
							if (empty($approve_no_desc)) {
								$this->show_warning('请输入退回理由');
								return;
							}
							$data['bulk_status']=BULK_STATUS_CREATE;
						}
					}
					break;
				case BULK_STATUS_OUTGOING_CONFIRM:
					if ($submit_val=="审批通过"){
						if ($user['role']==USER_FINANCE){
							$data['bulk_status']=BULK_STATUS_FINANCE_APPROVE;
						}
					}elseif ($submit_val=="审批不通过"){
						if ($user['role']==USER_FINANCE){
							if (empty($approve_no_desc)) {
								$this->show_warning('请输入审批不通过理由');
								return;
							}
							$data['bulk_status']=BULK_STATUS_CREATE;
						}
					}
					break;
				case BULK_STATUS_FINANCE_APPROVE:
					if ($submit_val=="确认提交"){
						if ($user['role']==USER_SOURCING){
							$data['bulk_status']=BULK_STATUS_CONFIRM;
						}
					}
					break;
			}
			$data['bulk_desction']=$bulk_desction;
			$this->_bulk_mod->edit($sour_id,$data);
			if ($this->_bulk_mod->has_error()){
				$this->show_warning($this->_bulk_mod->get_error());
				return;
			}
			$new_fabric_ids=array();
			if ($_POST['price']){
				foreach($_POST['price'] as $key=>$item)
				{
					$fabric_info_id=isset($_POST['fabric_info_id'][$key])?intval($_POST['fabric_info_id'][$key]):0;
					$fabric_data=array(
							"order_id"=>$sour_info['order_id'],
							"bulk_id"=>$sour_id,
							"bulk_status"=>$data['bulk_status'],
							"description"=>isset($_POST['description'][$key])?trim($_POST['description'][$key]):"",
							"fabric_shop_id"=>isset($_POST['fabric_shop_name'][$key])?intval($_POST['fabric_shop_name'][$key]):"",
							"cc_id"=>isset($_POST['cc_id'][$key])?intval($_POST['cc_id'][$key]):0,
							"item"=>isset($_POST['item'][$key])?trim($_POST['item'][$key]):"",
							"price"=>isset($_POST['price'][$key])?trim($_POST['price'][$key]):"",
							"colors"=>isset($_POST['colors'][$key])?trim($_POST['colors'][$key]):"",
							"fabric_num"=>isset($_POST['fabric_num'][$key])?trim($_POST['fabric_num'][$key]):"",
							"address"=>isset($_POST['address'][$key])?trim($_POST['address'][$key]):"",
							"phone_mob"=>isset($_POST['phone_mob'][$key])?trim($_POST['phone_mob'][$key]):"",
					);
	
					if(!empty($fabric_data['price']))
					{
						if(empty($fabric_info_id) || !in_array($fabric_info_id, $fabric_arr_id))
						{
							$fabric_info_mod->add($fabric_data);
						}
						else
						{
							$new_fabric_ids[]=$fabric_info_id;
	
							$fabric_info_mod->edit("fabric_info_id={$fabric_info_id}",$fabric_data);
						}
					}
				}
			}
			foreach($fabric_status_list as $item)
			{
				if(!in_array($item['fabric_info_id'],$new_fabric_ids))
				{
					$fabric_info_mod->drop("fabric_info_id={$item['fabric_info_id']}");
				}
			}
			$this->show_message('handle_success',Lang::get('bulk_sour_info'),'index.php?app=purchase&act=bulk_sour&id='.$sour_id);
		}
	}
	
	
	//头板采购查询
	function sourcing_list(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(20);
		$conditions=$this->_get_query_conditions(array(
			array(
				 'field' => 'sample_sn',
	             'equal' => 'like',
				 'name'	 => 'sample_sn'
			),
			array(
					'field' => 'merchandiser_id',
					'equal' => '=',
					'name'	 => 'merchandiser'
			),
			array(
					'field' => 'buyer_id',
					'equal' => '=',
					'name'	 => 'buyer'
			),
			array(
					'field' => 'sample.brand_id',
					'equal' => '=',
					'name'	 => 'brand_id'
			),
			array(
					'field' => 'sourcing_sn',
					'equal' => 'like',
					'name'	 => 'sourcing_sn'
			),
			array(
					'field' => 'sourcing_status',
					'equal' => '=',
					'name'	 => 'sourcing_status'
			)
		));
		$brand_mod =& m('brand');
		$this->assign('brand_list', $brand_mod->find(array('conditions'=>'if_show = 1','order'=>'brand_name','join'=>"belong_company",)));
		
		$buyer_mod =& m('member');
		$buyer_list = $buyer_mod->find(array(
				'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT,
				'order' => 'user_name asc',
		));
		$this->assign('buyer_list', $buyer_list);
		
		$merchandiser_list = $buyer_mod->find(array(
				'conditions' => 'if_show = 1 and role='.USER_MERCHANDISER,
				'order' => 'user_name asc',
		));
		$this->assign('merchandiser_list',$merchandiser_list);
			
		$conditions ="sourcing.order_id=0 and sourcing.sample_id<>0 and sample.status != " . SAMPLE_STATUS_DELETE.$conditions;
		$sour_list = $this->_sourcing_mod->get_sourcing_list($conditions,'add_time desc',$page['limit']);
		$page['item_count'] = $this->_sourcing_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('sour_list', $sour_list);
			
		$this->_curlocal(Lang::get('purchase_list'));
		$this->display('purchase.search.html');
	}
	
	
	//生产板采购查询
	function sourcing_status(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(20);
		$conditions=$this->_get_query_conditions(array(
				array(
						'field' => 'order_sn',
						'equal' => 'like',
						'name'	 => 'order_sn'
				),
				array(
						'field' => 'merchandiser_id',
						'equal' => '=',
						'name'	 => 'merchandiser'
				),
				array(
						'field' => 'buyer_id',
						'equal' => '=',
						'name'	 => 'buyer'
				),
				array(
						'field' => 'o.brand_id',
						'equal' => '=',
						'name'	 => 'brand_id'
				),
				array(
						'field' => 'sourcing_sn',
						'equal' => 'like',
						'name'	 => 'sourcing_sn'
				),
				array(
						'field' => 'sourcing_status',
						'equal' => '=',
						'name'	 => 'sourcing_status'
				)
		));
		$brand_mod =& m('brand');
		$this->assign('brand_list', $brand_mod->find(array('conditions'=>'if_show = 1','order'=>'brand_name','join'=>"belong_company",)));
		
		$buyer_mod =& m('member');
		$buyer_list = $buyer_mod->find(array(
				'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT,
				'order' => 'user_name asc',
		));
		$this->assign('buyer_list', $buyer_list);
		
		$merchandiser_list = $buyer_mod->find(array(
				'conditions' => 'if_show = 1 and role='.USER_MERCHANDISER,
				'order' => 'user_name asc',
		));
		$this->assign('merchandiser_list',$merchandiser_list);
			
		$conditions ="sourcing.order_id<>0 and sourcing.sample_id=0 ".$conditions;
		$sour_list = $this->_sourcing_mod->get_sourcing_status_list($conditions,'add_time desc',$page['limit']);
		$page['item_count'] = $this->_sourcing_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('sour_list', $sour_list);
		
		$this->_curlocal(Lang::get('purchase_order_list'));
		$this->display('purchase.order_list.html');
	}
	
	
	//大货采购单查询
	function sourcing_bulk(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(20);
		$conditions=$this->_get_query_conditions(array(
				array(
						'field' => 'order_sn',
						'equal' => 'like',
						'name'	 => 'order_sn'
				),
				array(
						'field' => 'merchandiser_id',
						'equal' => '=',
						'name'	 => 'merchandiser'
				),
				array(
						'field' => 'buyer_id',
						'equal' => '=',
						'name'	 => 'buyer'
				),
				array(
						'field' => 'o.brand_id',
						'equal' => '=',
						'name'	 => 'brand_id'
				),
				array(
						'field' => 'bulk_sn',
						'equal' => 'like',
						'name'	 => 'bulk_sn'
				),
				array(
						'field' => 'bulk_status',
						'equal' => '=',
						'name'	 => 'bulk_status'
				)
		));
		$brand_mod =& m('brand');
		$this->assign('brand_list', $brand_mod->find(array('conditions'=>'if_show = 1','order'=>'brand_name','join'=>"belong_company",)));
		
		$buyer_mod =& m('member');
		$buyer_list = $buyer_mod->find(array(
				'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT,
				'order' => 'user_name asc',
		));
		$this->assign('buyer_list', $buyer_list);
		
		$merchandiser_list = $buyer_mod->find(array(
				'conditions' => 'if_show = 1 and role='.USER_MERCHANDISER,
				'order' => 'user_name asc',
		));
		$this->assign('merchandiser_list',$merchandiser_list);
			
		if ($user_info['role']==USER_SOURCING){
			$conditions .=" and bulk_status".db_create_in(array(BULK_STATUS_FINANCE_APPROVE,BULK_STATUS_CONFIRM));
		}
		$sour_list = $this->_bulk_mod->find(array(
				'join'=>'belongs_to_bulk',
				'conditions'=>'1=1 '.$conditions,
				'count'=>true,
				'limit'=>$page['limit'],
				'order'=>'bulk_time desc'
		));
		$page['item_count'] = $this->_bulk_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('sour_list', $sour_list);
		
		$this->_curlocal(Lang::get('purchase_bulk_list'));
		$this->display('purchase.bulk_list.html');
	}
	
	
	//打印生产板采购单
	function print_purchase(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$sour_mod=&m('sourcing_info');
		$user_info=$this->visitor->get();
		if (!$id) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		$orders = $sour_mod->get(array(
				'conditions' => 'sourcing_id = ' . $id,
				'join' => 'belongs_order_sour',
		));
		if (!$orders) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		if($user_info['role']!=USER_MERCHANDISER && $user_info['role']!=USER_SOURCING)
		{
			$this->show_warning('用户无权限打印此采购单');
			return;
		}
	
			
		$fabric_order_mod =& m('fabric_order');
		$fabric_order_list = $fabric_order_mod->find(array(
				'conditions'=>'sourcing_id = ' . $id.' and order_id='.$orders['order_id'],
				'fields'=>"fabric_order.fabric_info_id,fabric_order.description,fabric_order.fabric_shop_id,item,price,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id",
				'join'	=>"belongs_fabric_shop",
				'order'=>'fabric_info_id ASC'
		));
		$this->assign('fabric_info_list', $fabric_order_list);
	
		//赋值用户信息
		$this->assign("user",$user_info);
			
		if (empty($orders['if_print'])){
			$sour_mod->edit($id,array('if_print'=>1));
			if ($sour_mod->has_error()){
				$this->show_warning($sour_mod->get_error());
				return;
			}
		}
		$this->assign('sours', $orders);
		$this->_curlocal(Lang::get('sourcing_info'), 'index.php?app=purchase&act=print_purchase&id='.$id, Lang::get('sourcing_info'));
		$this->display('purchase_order.print.html');
	}
	
	
	//打印大货采购单
	function print_bulk(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$sour_mod=&m('bulk_sourcing');
		$user_info=$this->visitor->get();
		if (!$id) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		$orders = $sour_mod->get(array(
				'conditions' => 'bulk_id = ' . $id,
				'join' => 'belongs_to_bulk',
		));
		if (!$orders) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		if($user_info['role']!=USER_SOURCING)
		{
			$this->show_warning('用户无权限打印此采购单');
			return;
		}
	
			
		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
				'conditions'=>'order_id='.$orders['order_id'],
				'fields'=>"fabric_info.fabric_info_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,fabric_shop_name,colors,fabric_num,phone_mob,address,cc_id",
				'join'	=>"belongs_fabric_shop",
				'order'=>'fabric_info_id ASC'
		));
		$this->assign('fabric_info_list', $fabric_info_list);
	
		//赋值用户信息
		$this->assign("user",$user_info);
			
		if (empty($orders['if_print'])){
			$sour_mod->edit($id,array('if_print'=>1));
			if ($sour_mod->has_error()){
				$this->show_warning($sour_mod->get_error());
				return;
			}
		}
		$this->assign('sours', $orders);
		$this->_curlocal(Lang::get('sourcing_info'), 'index.php?app=purchase&act=print_bulk&id='.$id, Lang::get('sourcing_info'));
		$this->display('purchase_bulk.print.html');
	}
	
	//上传头板凭证
	function upload_invoices(){
		$s_id=empty($_GET['s_id'])?0:intval($_GET['s_id']);
		$f_id=empty($_GET['fid'])?0:intval($_GET['fid']);
		$user_info=$this->visitor->get();
		if (!$s_id || !$f_id) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$this->assign('user',$user_info);
		$sour_mod=&m('sourcing_info');
		$sample = $sour_mod->get(array(
				'fields' => '*,sample.description',
				'conditions' => 'sourcing_id = ' . $s_id.' and sample.status='.SAMPLE_STATUS_SEND_FACTORY,
				'join' => 'belongs_sample',
		));
		if (!$sample) {
			$this->show_warning('sample_not_exist');
			return;
		}
		$fabric_mod=&m('fabric_info');
		$fabric_info=$fabric_mod->get(array(
			'conditions'=>'fabric_info_id='.$f_id	
		));
		if (empty($fabric_info)){
			$this->show_warning('fabric_not_exist');
			return;
		}
		$file_mod=&m('upload');
		$sample['sample_sour_upload'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SOURCING_INVOICES . ' AND key_id= ' .$fabric_info['fabric_info_id'],
				'order' => 'file_id ASC',
		));
		$this->assign('belongs_sour_invoice',BELONGS_SOURCING_INVOICES);
		$this->assign('sample',$sample);
		$this->assign('fabric_info',$fabric_info);
		$this->display('sourcing.handle.html');
	}
	
	//上传生产板凭证
	function upload_order_invoices(){
		$s_id=empty($_GET['s_id'])?0:intval($_GET['s_id']);
		$f_id=empty($_GET['fid'])?0:intval($_GET['fid']);
		if (!$s_id || !$f_id) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		$sour_info = $this->_sourcing_mod->get(array(
				'conditions' => 'sourcing_id = ' . $s_id,
				'join' => 'belongs_order_sour',
		));
		if (empty($sour_info)) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		$fabric_mod=&m('fabric_order');
		$fabric_info=$fabric_mod->get(array(
				'conditions'=>'fabric_info_id='.$f_id
		));
		if (empty($fabric_info)){
			$this->show_warning('fabric_not_exist');
			return;
		}
		$file_mod=&m('upload');
		$sour_info['sample_sour_upload'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_ORDER_SOURCING_INVOICES . ' AND key_id= ' .$fabric_info['fabric_info_id'],
				'order' => 'file_id ASC',
		));
		$this->assign('belongs_sour_invoice',BELONGS_ORDER_SOURCING_INVOICES);
		$this->assign('sample',$sour_info);
		$this->assign('fabric_info',$fabric_info);
		$this->display('sourcing.handle.html');
	}
	
	
	//上传大货采购单凭证
	function upload_bulk_invoices(){
		$s_id=empty($_GET['s_id'])?0:intval($_GET['s_id']);
		$f_id=empty($_GET['fid'])?0:intval($_GET['fid']);
		if (!$s_id || !$f_id) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		$sour_info = $this->_bulk_mod->get(array(
				'conditions' => 'bulk_id = ' . $s_id,
				'join' => 'belongs_to_bulk',
		));
		if (empty($sour_info)) {
			$this->show_warning('sourcing_not_exist');
			return;
		}
		$fabric_mod=&m('fabric_info');
		$fabric_info=$fabric_mod->get(array(
				'conditions'=>'fabric_info_id='.$f_id
		));
		if (empty($fabric_info)){
			$this->show_warning('fabric_not_exist');
			return;
		}
		$file_mod=&m('upload');
		$sour_info['sample_sour_upload'] = $file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_BULK_INVOICES . ' AND key_id= ' .$fabric_info['fabric_info_id'],
				'order' => 'file_id ASC',
		));
		$this->assign('belongs_sour_invoice',BELONGS_BULK_INVOICES);
		$this->assign('sample',$sour_info);
		$this->assign('fabric_info',$fabric_info);
		$this->display('sourcing.handle.html');
	}
	
	//审批不通过
	function approve_not_pass()
	{
		$this->display('sourcing.approve_not_pass.html');
	}
}